package com.yongbao.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yongbao.common.PageInfo;
import com.yongbao.common.utils.AssertUtil;
import com.yongbao.model.dtomapper.DishSpecificationDtoMapper;
import com.yongbao.model.domain.DishSpecificationDO;
import com.yongbao.mapper.DishSpecificationMapper;
import com.yongbao.model.request.DishSpecificationRequest;
import com.yongbao.model.util.DishSpecificationConvert;
import com.yongbao.service.DishSpecificationService;
import com.yongbao.model.vo.DishSpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class DishSpecificationServiceImpl extends ServiceImpl<DishSpecificationMapper, DishSpecificationDO> implements DishSpecificationService {
    @Autowired
    private DishSpecificationMapper dishSpecificationMapper;

    @Override
    public DishSpecificationVO getByKey(long id) {
        DishSpecificationVO dishSpecificationVO = DishSpecificationDtoMapper.INSTANCE.do2VO(getById(id));
        return dishSpecificationVO;
    }

    @Override
    public PageInfo<DishSpecificationVO> pageQuery(DishSpecificationRequest request) {
        long count = dishSpecificationMapper.pageCount(request);
        List<DishSpecificationDO> list = dishSpecificationMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<DishSpecificationVO> dishSpecificationVOList = DishSpecificationConvert.buildDishSpecificationVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), dishSpecificationVOList, count);
    }

    @Override
    public List<DishSpecificationVO> listQuery(DishSpecificationRequest request) {
        List<DishSpecificationDO> list = dishSpecificationMapper.selectDOList(DishSpecificationDtoMapper.INSTANCE.request2DO(request));
        List<DishSpecificationVO> dishSpecificationVOS = DishSpecificationConvert.buildDishSpecificationVoList(list);
        return dishSpecificationVOS;
    }

    @Override
    public Boolean add(DishSpecificationRequest request) {
        save(DishSpecificationDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean updateByKey(DishSpecificationRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(DishSpecificationDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean delByKey(String ids) {
        dishSpecificationMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
